package com.btks.jswater.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.btks.jswater.entity.WaterAllPoint;
import com.btks.jswater.entity.WaterMoniData;
import com.btks.jswater.mapper.WaterMoniDataMapper;
import com.btks.jswater.service.IWaterMoniDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ZhangKewen
 * @since 2022-05-06
 */
@Service
public class WaterMoniDataServiceImpl extends ServiceImpl<WaterMoniDataMapper, WaterMoniData> implements IWaterMoniDataService {
    @Resource
    private WaterMoniDataMapper waterMoniDataMapper;

    public void insertMoniData(String code, String name, String type, String tm,
                               BigDecimal w, BigDecimal q, BigDecimal l, String adcode) {

        waterMoniDataMapper.insertMoniData(code, name, type, tm, w, q, l, adcode);
    }


    public boolean isValidDate(String strDate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setLenient(false);
        sdf.parse(strDate);
        return true;
    }
    public boolean isNumeric(String str){
        Pattern pattern = Pattern.compile("[0-9]*\\.?[0-9]+");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    /**
     * 缓冲区查询
     *  * **/
    public IPage<WaterAllPoint> getBuffer(float x, float y, int page, int pageSize){
        Page<WaterAllPoint> p = new Page<>(page, pageSize);
        p.setRecords( waterMoniDataMapper.getBuffer(p,x,y));
       return p;
    }
    /**
     * 名称模糊查询
     *  * **/
    public IPage<WaterAllPoint> getByName(String keywords,int page, int pageSize){
        Page<WaterAllPoint> p = new Page<>(page, pageSize);
        p.setRecords( waterMoniDataMapper.getByName(p,keywords));
        return p;}
    /**
     * 类型查询
     *  * **/
    public WaterAllPoint getByStcd(String stcd){return waterMoniDataMapper.getByStcd(stcd);}

    /**
     * 根据测站号和时间查询数据库是否已经存储
     *  * **/
    public int dataIfExists(String stcd,String tm){
        return waterMoniDataMapper.dataIfExists(stcd,tm);
    }
    /**
     * 根据测站号和时间更新数据
     *  * **/
    public  void updataByTmCode(String stcd,String tm,BigDecimal stl,BigDecimal stq,BigDecimal stw){
        waterMoniDataMapper.updataByTmCode(stcd, tm, stl, stq, stw);}
}
